import { DirectionCode, KeyEventParameters, SceneUtils, TButton, TLayout } from "../SceneControl";
import MainPageScene from "./MainPageScene";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChapterViewScene extends MainPageScene {
    upLayout: TLayout;
    Left: cc.Node;
    Right: cc.Node;
    stage: cc.Node;
    Sweep: cc.Node;
    Battle: cc.Node;
    Activity: cc.Node;
    waitfresh: boolean;
    Award: cc.Node;

    protected onEnable(): void {
        this.Left = cc.find("btnLeft", this.node);
        this.Right = cc.find("btnRight", this.node);
        this.Sweep = cc.find("btnSweep", this.node);
        this.Award = cc.find("award", this.node);
        this.Battle = cc.find("btnBattle", this.node);
        this.Activity = cc.find("btnActivity", this.node);
        this.stage = cc.find("chapter/txtChapter", this.node);
        super.onEnable();
        this.Left.on("active-in-hierarchy-changed", this.refresh, this);
        this.Right.on("active-in-hierarchy-changed", this.refresh, this);
        this.Activity.on("active-in-hierarchy-changed", this.refresh, this);
        this.Sweep.on("active-in-hierarchy-changed", this.refresh, this);
        this.Battle.on("active-in-hierarchy-changed", this.refresh, this);
        this.Award.on(cc.Node.EventType.CHILD_ADDED, this.refresh, this);
        this.Award.on(cc.Node.EventType.CHILD_REMOVED, this.refresh, this);
    }

    protected onDisable(): void {
        super.onDisable();
        this.Left.off("active-in-hierarchy-changed", this.refresh, this);
        this.Right.off("active-in-hierarchy-changed", this.refresh, this);
        this.Activity.off("active-in-hierarchy-changed", this.refresh, this);
        this.Sweep.off("active-in-hierarchy-changed", this.refresh, this);
        this.Battle.off("active-in-hierarchy-changed", this.refresh, this);
        this.Award.off(cc.Node.EventType.CHILD_ADDED, this.refresh, this);
        this.Award.off(cc.Node.EventType.CHILD_REMOVED, this.refresh, this);
    }

    getUpLayout(): TLayout {
        this.upLayout = new TLayout(this.node.name);
        let btns = this.btns;
        this.upLayout.buttonList = btns;
        this.upLayout.site.row = btns.length - 1;
        return this.upLayout;
    }

    refresh() {
        if (this.waitfresh)
            return;
        this.waitfresh = true;
        this.scheduleOnce(() => {
            this.waitfresh = false;
            this.upLayout.buttonList = this.btns
            this.tscene.apply();
        }, 0.1)
    }

    get btns() {
        let nodes: TButton[][] = [[], [], []];
        let result: TButton[][] = [];
        nodes[0].push(new TButton(this.stage));
        nodes[0][0].addKeyDownEvent((para: KeyEventParameters) => {
            if (para.DirectionCode == DirectionCode.LEFT) {
                this.Left.activeInHierarchy && SceneUtils.emitButton(this.Left)
            } else if (para.DirectionCode == DirectionCode.RIGHT) {
                this.Right.activeInHierarchy && SceneUtils.emitButton(this.Right)
            }
        })

        cc.find("award", this.node).children.forEach(item => item.getComponent(cc.Button) && nodes[1].push(new TButton(item)));
        this.Sweep.activeInHierarchy && nodes[2].push(new TButton(this.Sweep));
        this.Battle.activeInHierarchy && nodes[2].push(new TButton(this.Battle));
        this.Activity.activeInHierarchy && nodes[2].push(new TButton(this.Activity));

        nodes.forEach(arr => {
            if (arr.length > 0) {
                result.push(arr);
            }
        })
        return result;
    }
}
